

package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.net.Socket;
	import flash.ui.ContextMenu;
	import flash.utils.ByteArray;
	
	import mvc.MFacade;
	import mvc.command.start.startGameCommand;
	import mvc.model.start.initInfo;
	import mvc.model.start.stageResizeVo;
	import mvc.notification.start.gameStartNotification;
	
	public class GDWeb extends Sprite
	{
		private var _init:Boolean = false;
		
		private var _socket:Socket;
		
		public function GDWeb()
		{
			this.contextMenu = new ContextMenu ();
			this.contextMenu.hideBuiltInItems();
			
			_socket = new Socket();
			_socket.addEventListener(Event.CONNECT,onConnect);
//			_socket.connect("192.168.0.3",8888);
			
			addEventListener(Event.ADDED_TO_STAGE,onAddtostage);
		}
		
		private function onConnect(e:Event):void
		{
			trace("the socket is connect ...");			
			_socket.writeUTFBytes("aaaaaaa");
		}
		
		private function onAddtostage(e:Event):void
		{
			stage.addEventListener(Event.RESIZE,onStageResize);
			stage.addEventListener(Event.ENTER_FRAME,onEnterframe);
		}
		
		private function onStageResize(e:Event):void
		{
			if(stage.stageWidth > 0 && stage.stageHeight > 0)
			{
				var vo:stageResizeVo = new stageResizeVo();
				vo._width = stage.stageWidth;
				vo._height = stage.stageHeight;
				
				MFacade.instance.sendNotification(gameStartNotification.stageResizeCommand,vo);
			}
		}
		
		private function onEnterframe(e:Event):void
		{
			stage.removeEventListener(Event.ENTER_FRAME,onEnterframe);
			
			init();
		}
		
		private function init():void
		{
			if(!_init)
			{
				stage.scaleMode = StageScaleMode.NO_SCALE;
				stage.align = StageAlign.TOP_LEFT;
				
				MFacade.instance.registerCommand(gameStartNotification.gameStartCommand,startGameCommand);
				
				var _t:initInfo = new initInfo();
				_t._stage = this.stage;
				_t._width = stage.stageWidth;
				_t._height = stage.stageHeight;
				
				MFacade.instance.sendNotification(gameStartNotification.gameStartCommand,_t);
				
				_init = true;
			}
		}
	}
}